$(function(){
	/**
	 * 权限约束控制按钮
	 */
	$("a[name=auth_config_btn]").click(function(){
		var resourceId= $(this).attr("data-id");
		$("#auth_form input[name=resourceId]").val(resourceId);
		var authType= $(this).attr("data-authType");
		//.iCheck('check');  使用美化皮肤后必须要用icheck提供的选中方法
		if(authType=="1"){
			$("#auth-def").attr("checked",true);
		}else if(authType=="2"){
			$("#auth-login").attr("checked",true);
		}else if(authType=="3"){
			$("#auth-anon").attr("checked",true);
		}
		admin.dialog_show("权限控制","#myModal","#auth_form",500,300);
	});

	/**
	 * 停用资源访问
	 */
	$("a[name=stop_btn]").click(function(){
		var resourceId = $(this).attr("data-id");
		updateResourceStatus("stop", resourceId);
	});

	$("a[name=delete_btn]").click(function(){
		var resourceId = $(this).attr("data-id");
		updateResourceStatus("del",resourceId);
	});
	/**
	 * 开启资源访问
	 */
	$("a[name=start_btn]").click(function(){
		var resourceId = $(this).attr("data-id");
		updateResourceStatus("start", resourceId);
	});
	function updateResourceStatus(status,resourceId){
		var url,msg;
		if("stop"==status){
			url=htx+"/res/stopAccess";
			msg="您确定要停止当前资源的访问服务吗？";
		}else if("start"==status){
			url=htx+"/res/startAccess";
			msg="您确定要开启当前资源的访问服务吗？";
		}else{
			url=htx+"/res/deleteAccess";
			msg="您确定要删除当前资源的访问服务吗？";
		}
		layer.confirm(msg, {
		    btn: ["确定","取消"] //按钮
		},
		//确定btn
		function(){
			var index = layer.load();
			$.ajax({
				url:url,
				type:"POST",
				data:{"resourceId":resourceId},
				dataType:"JSON",
				success:function(data){
					layer.close(index);
					if(data.status=="T"){
						layer.msg(data.info,{time:1000},function(){$("#formsearch").submit();});
					}else{
						layer.msg(data.info);
					}
				}
			});
		},
		//取消btn
		function(){

		});
	}
	/**
	 * 权限约束保存按钮
	 */
	$("#saveAuthBtn").click(function(){
		layer.confirm("您确定要修改当前资源的权限控制？", {
		    btn: ["确定","取消"] //按钮
		},
		//确定btn
		function(){
			var index = layer.load();
			var resourceId = $("#auth_form input[name=resourceId]").val();
			var authType= $("#auth_form input[name=authType]:checked").val();
			$.ajax({
				url:htx+"/res/saveAuthType",
				type:"POST",
				data:{"resourceId":resourceId,"authType":authType},
				dataType:"JSON",
				success:function(data){
					layer.close(index);
					if(data.status=="T"){
						layer.msg(data.info,{time:1000},function(){$("#formsearch").submit();});
					}else{
						layer.msg(data.info);
					}
				}
			});
		},
		//取消btn
		function(){

		});
	});
	/**
	 * 其它约束控制按钮
	 */
	$("a[name=access_constraint_btn]").click(function(){
		admin.dialog_show("访问控制","#myModal","#access_constraint",700,500);
	});

	/**
	 * 刷新当前系统的访问资源
	 */
	$("#refreshResource").click(function(){
		layer.confirm(
			"您确定要刷新当前系统访问资源？",
			{
				btn: ["确定","取消"] //按钮
			},
			//确定btn
			function(){
				var index = layer.load();
				$.ajax({
					url:htx+"/sys/refreshResource",
					type:"POST",
					dataType:"JSON",
					success:function(data){
						layer.close(index);
						if(data.status=="T"){
							layer.msg(data.info,{time:1000},function(){$("#formsearch").submit();});
						}else{
							layer.msg(data.info);
						}
					}
				});
			},
			//取消btn
			function(){

			}
		);
	});
});